// 获取应用实例
const app = getApp()
var http = require('../../utils/request');
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: true,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && !wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    showModal: false,
    openinfo: {},
    retmsg: ""
  },
  onLoad() {
    this.isAuthorize();
    this.getOpenInfo();
  },
  isAuthorize() {
    // let openinfo = wx.getStorageSync('openinfo');
    // let mobile = wx.getStorageSync('mobile');
    // if (mobile) {
    //   this.getToken(mobile);
    // }
    // if (openinfo && openinfo.openid && ! mobile) {
    //   this.setData({showModal: true});
    //   this.setData({
    //     canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName')
    //   })
    //   this.setData({hasUserInfo: true});
    // } else if (openinfo && openinfo.openid && mobile) {
    //   this.setData({
    //     canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName')
    //   })
    //   this.setData({hasUserInfo: true});
    // } else {
    //   this.setData({
    //     canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && !wx.canIUse('open-data.type.userNickName')
    //   });
    // }
    let mobile = wx.getStorageSync('mobile');
    if (mobile) {
      this.getToken(mobile);
    }
  },
  getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({userInfo: res.userInfo, hasUserInfo: true});
        app.globalData.userInfo = res.userInfo;
        wx.setStorageSync('wechat_userinfo', res.userInfo);
        this.getOpenInfo();
      }
    })
  },
  getOpenInfo() {
    var that = this
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求
          // 使用 code 换取 openid 和 session_key 等信息
          http.postData('/xiandu/app/v1/get_code2session', {
            code: res.code
          }, (rep) => {
            if (rep.code == 200200) {
              that.setData({openinfo: rep.data, hasUserInfo: true});
              wx.setStorageSync('openinfo', rep.data);
              that.photoToast();
              return;
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  getUserInfo(e) { // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({userInfo: e.detail.userInfo, hasUserInfo: true});
    app.globalData.userInfo = e.detail.userInfo;
    wx.setStorageSync('wechat_userinfo', e.detail.userInfo);
    this.getOpenInfo();
  },
  // 手机验证弹框
  photoToast() {
    this.showDialogBtn();
  },
  showDialogBtn: function () {
    var that = this
    that.setData({showModal: true})
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    var that = this
    that.setData({showModal: false});
  },
  getPhoneNumber(e) {
    let that = this;
    let session_key = wx.getStorageSync('openinfo').session_key;
    // if (!e.detail.iv) { // 判断是否允许授权，拿到加密信息，展示授权弹框
    //   that.setData({showModal: false})
    //   return;
    // }
    http.postData('/xiandu/app/v1/get_decode_userinfo', {
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData,
      session_key: session_key
    }, (rep) => {
      if (rep.code == 200200) {
        this.getToken(rep.data.phoneNumber);
        wx.setStorageSync('mobile', rep.data.phoneNumber);
      }
    })
  },
  getToken(item) {
    http.postData('/xiandu/app/v1/get_apptoken', {
      mobile: item
    }, (rep) => {
      if (rep.code == 200200) {
        wx.setStorageSync('userinfo', rep.data);
        wx.reLaunch({
          url: '/pages/index/index'
        })
      } else if (rep.code == 200204) {
        this.setData({retmsg: rep.msg});
      }
      // this.setData({showModal: false});
    })
  }
})
